#include "ControlSystem.hpp"
#include <GQE/Entity/classes/Instance.hpp>

ControlSystem::ControlSystem(GQE::IApp& theApp):
  ISystem("ControlSystem",theApp)
{

}
ControlSystem::~ControlSystem()
{

}
void ControlSystem::AddProperties(GQE::IEntity* theEntity)
{
  theEntity->mProperties.Add<float>("fSpeed",4.0f);
  theEntity->mProperties.Add<GQE::Uint32>("uHealth",100);
  theEntity->mProperties.Add<GQE::Uint32>("uMaxHealth",100);
  theEntity->mProperties.Add<sf::Vector2f>("vStartPosition",sf::Vector2f(0,0));
  theEntity->mProperties.Add<bool>("bImmune",true);
  theEntity->mProperties.Add<bool>("bDead",false);
  theEntity->mProperties.Add<bool>("bPlayer",false);
  theEntity->mProperties.Add<bool>("bWarp",false);
  theEntity->mProperties.Add<sf::Clock>("ImmuneTimer",sf::Clock());
  theEntity->mProperties.Add<sf::Clock>("DeadTimer",sf::Clock());
  theEntity->mProperties.Add<bool>("bGreenKey",false);
  theEntity->mProperties.Add<bool>("bBlueKey",false);
  theEntity->mProperties.Add<bool>("bRedKey",false);
  theEntity->mProperties.Add<bool>("bBow",false);
  theEntity->mProperties.Add<GQE::Uint32>("uArrows",0);
}

void ControlSystem::HandleInit(GQE::IEntity* theEntity)
{
}

void ControlSystem::HandleEvents(sf::Event theEvent)
{
}

void ControlSystem::UpdateFixed()
{
  // Search through each z-order map to find IEntity registered
  std::map<const GQE::typeEntityID, std::deque<GQE::IEntity*> >::iterator anIter;
  anIter = mEntities.begin();
  while(anIter != mEntities.end())
  {
    std::deque<GQE::IEntity*>::iterator anQueue = anIter->second.begin();
    while(anQueue != anIter->second.end())
    {
      // Get the IEntity address first
      GQE::IEntity* anEntity = *anQueue;

      // Increment the IEntity iterator second
      anQueue++;

      if(anEntity->mProperties.Get<GQE::Uint32>("uHealth") >
         anEntity->mProperties.Get<GQE::Uint32>("uMaxHealth"))
      {
        anEntity->mProperties.Set<GQE::Uint32>("uHealth",
          anEntity->mProperties.Get<GQE::Uint32>("uMaxHealth"));
      }
      // Get the current control system properties from this IEntity
      float anSpeed = anEntity->mProperties.Get<float>("fSpeed");
      sf::IntRect anFrameRect = anEntity->mProperties.Get<sf::IntRect>("rSpriteRect");
      sf::Vector2f anVelocity(0,0);
      if(!anEntity->mProperties.Get<bool>("bDead"))
      {
        // Check the keyboard to compute the new velocity value
#if SFML_VERSION_MAJOR<2
        if(mApp.mInput.IsKeyDown(sf::Key::Left))
        {			
          anVelocity.x=-anSpeed;	
          // GetHeight changes as we change top and bottom, so cache its value first
          int anHeight = anFrameRect.GetHeight();
          anFrameRect.Top=anHeight*3;
          anFrameRect.Bottom=anHeight*4;
					anEntity->mProperties.Set<sf::Vector2u>("wFrameModifier",sf::Vector2u(1,0)); 
        }
        else if(mApp.mInput.IsKeyDown(sf::Key::Right))
        {
          anVelocity.x=anSpeed;
          // GetHeight changes as we change top and bottom, so cache its value first
          int anHeight = anFrameRect.GetHeight();
          anFrameRect.Top=anHeight;
          anFrameRect.Bottom=anHeight*2;
          anEntity->mProperties.Set<sf::Vector2u>("wFrameModifier",sf::Vector2u(1,0));
        }			
        if(mApp.mInput.IsKeyDown(sf::Key::Up))
        {
          anVelocity.y=-anSpeed;
          // GetHeight changes as we change top and bottom, so cache its value first
          int anHeight = anFrameRect.GetHeight();
          anFrameRect.Top=0;
          anFrameRect.Bottom=anHeight;
          anEntity->mProperties.Set<sf::Vector2u>("wFrameModifier",sf::Vector2u(1,0));
        }			
        else if(mApp.mInput.IsKeyDown(sf::Key::Down))
        {	
          anVelocity.y=anSpeed;
          // GetHeight changes as we change top and bottom, so cache its value first
          int anHeight = anFrameRect.GetHeight();
          anFrameRect.Top=anHeight*2;
          anFrameRect.Bottom=anHeight*3;
          anEntity->mProperties.Set<sf::Vector2u>("wFrameModifier",sf::Vector2u(1,0));
        }
        else
        {
					anEntity->mProperties.Set<sf::Vector2u>("wFrameModifier",sf::Vector2u(0,0));
					anEntity->mProperties.Set<sf::IntRect>("rSpriteRect",sf::IntRect(0,anFrameRect.top,32,anFrameRect.top+32));
				}
#else
        if(sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
        {			
          anVelocity.x=-anSpeed;	
          anFrameRect.top=anFrameRect.height*3;
          anEntity->mProperties.Set<sf::Vector2u>("wFrameModifier",sf::Vector2u(1,0));
        }
        else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
        {
          anVelocity.x=anSpeed;
          anFrameRect.top=anFrameRect.height;
          anEntity->mProperties.Set<sf::Vector2u>("wFrameModifier",sf::Vector2u(1,0));
        }			
        if(sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
        {
          anVelocity.y=-anSpeed;
          anFrameRect.top=0;
          anEntity->mProperties.Set<sf::Vector2u>("wFrameModifier",sf::Vector2u(1,0));
        }			
        if(sf::Keyboard::isKeyPressed(sf::Keyboard::Down))
        {	
          anVelocity.y=anSpeed;
          anFrameRect.top=anFrameRect.height*2;
					anEntity->mProperties.Set<sf::Vector2u>("wFrameModifier",sf::Vector2u(1,0));
        }
        if(!sf::Keyboard::isKeyPressed(sf::Keyboard::Down)&&!sf::Keyboard::isKeyPressed(sf::Keyboard::Up)&&!sf::Keyboard::isKeyPressed(sf::Keyboard::Left)&&!sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
        {
					anEntity->mProperties.Set<sf::Vector2u>("wFrameModifier",sf::Vector2u(0,0));
					anEntity->mProperties.Set<sf::IntRect>("rSpriteRect",sf::IntRect(32,anFrameRect.top,32,32));
				}
#endif
#if SFML_VERSION_MAJOR<2
        if(mApp.mInput.IsKeyDown(sf::Key::A))
        {			
          if(anFrameRect.Top==0)
          {

          }
          if(anFrameRect.Top==anFrameRect.GetHeight())
          {

          }
          if(anFrameRect.Top==anFrameRect.GetHeight()*2)
          {

          }
          if(anFrameRect.Top==anFrameRect.GetHeight()*3)
          {

          }
        }
#else
        if(sf::Keyboard::isKeyPressed(sf::Keyboard::A))
        {			
          if(anFrameRect.top==0)
          {

          }
          if(anFrameRect.top==anFrameRect.height)
          {

          }
          if(anFrameRect.top==anFrameRect.height*2)
          {

          }
          if(anFrameRect.top==anFrameRect.height*3)
          {

          }
        }
#endif
        // Now update the control system properties for this IEntity
        anEntity->mProperties.Set<sf::Vector2f>("vVelocity",anVelocity);
        anEntity->mProperties.Set<sf::IntRect>("rSpriteRect",anFrameRect);
      }
      else
      {
        anEntity->mProperties.Set<sf::Vector2f>("vVelocity",sf::Vector2f(0,0));
        anEntity->mProperties.Set<sf::Vector2u>("wFrameModifier",sf::Vector2u(0,1));
        anEntity->mProperties.Set<sf::IntRect>("rFrameRect",sf::IntRect(48,0,16,64));
        sf::Clock anDeadTimer=anEntity->mProperties.Get<sf::Clock>("DeadTimer");
#if SFML_VERSION_MAJOR<2
        if(anDeadTimer.GetElapsedTime() > 3.0f)
#else
        if(anDeadTimer.getElapsedTime().asSeconds() > 3.0f)
#endif
        {
          anEntity->mProperties.Set<bool>("bDead",false);
          anEntity->mProperties.Set<sf::Vector2u>("wFrameModifier",sf::Vector2u(1,0));
          anEntity->mProperties.Set<sf::IntRect>("rFrameRect",sf::IntRect(0,0,32,64));
          anEntity->mProperties.Set<sf::Uint32>("uHealth",100);
          anEntity->mProperties.Set<sf::IntRect>("rSpriteRect",sf::IntRect(0,0,16,16));
          anEntity->mProperties.Set<sf::Vector2f>("vPosition",anEntity->mProperties.Get<sf::Vector2f>("vStartPosition"));
        }
      }
      if(anEntity->mProperties.Get<bool>("bImmune"))
      {
        if(anEntity->mProperties.Get<bool>("bVisible"))
        {
          anEntity->mProperties.Set<bool>("bVisible",false);
        }
        else
        {
          anEntity->mProperties.Set<bool>("bVisible",true);
        }
        sf::Clock anImmuneTimer = anEntity->mProperties.Get<sf::Clock>("ImmuneTimer");
#if SFML_VERSION_MAJOR<2
        if(anImmuneTimer.GetElapsedTime() > 1.0f)
#else
        if(anImmuneTimer.getElapsedTime().asSeconds() > 1.0f)
#endif
        {
          anEntity->mProperties.Set<bool>("bImmune",false);
          anEntity->mProperties.Set<bool>("bVisible",true);
        }
      }
    }

    // Increment map iterator
    anIter++;
  } //while(anIter != mEntities.end())
}

void ControlSystem::UpdateVariable(float theElaspedTime)
{
}

void ControlSystem::Draw()
{
}

void ControlSystem::HandleCleanup(GQE::IEntity* theEntity)
{
}
